﻿using System;
using System.Configuration;
using System.Collections.Generic;
using StockTrader.DAL.IDAL;
using StockTrader.DAL.Factory;
using StockTrader.BusinessService.DataContract;
using StockTrader.FrontEnd.DataContract;
using System.Data;
using System.Reflection;

namespace StockTrader.FrontEnd.MarketViewer
{
    public class DALclient
    {
        private IAllMarket dalAllMarket;
        private ISecurities dalSecurities;

        public void InsertAllMarket(string orderNo, string side, string symbol, string price, long volume, long mVolume, string PC, string accountNo, string status, string TID, string Tno, string time)
        {
            dalAllMarket = StockTrader.DAL.Factory.AllMarketFactory.Create(ConfigurationManager.AppSettings["DbType"]);
            dalAllMarket.Open(ConfigurationManager.AppSettings["SQLserverCnnStr"]);
            try
            {
                dalAllMarket.InsertAllMarket(orderNo, side, symbol, price, volume, mVolume, PC, accountNo, status, TID, Tno, time);

            }
            catch
            {
                throw;
            }
            finally
            {
                dalAllMarket.Close();
            }

        }

        public void InsertBackupAllMarket(DateTime transDate, string orderNo, string side, string symbol, string price, long volume, long mVolume, string PC, string accountNo, string status, string TID, string Tno, string time)
        {
            dalAllMarket = StockTrader.DAL.Factory.AllMarketFactory.Create(ConfigurationManager.AppSettings["DbType"]);
            dalAllMarket.Open(ConfigurationManager.AppSettings["SQLserverCnnStr"]);
            try
            {
                dalAllMarket.InsertBackupAllmarket(transDate, orderNo, side, symbol, price, volume, mVolume, PC, accountNo, status, TID, Tno, time);

            }
            catch
            {
                throw;
            }
            finally
            {
                dalAllMarket.Close();
            }

        }

        public void InsertHoseMarketFromRawData(DateTime transDate)
        {
            dalAllMarket = StockTrader.DAL.Factory.AllMarketFactory.Create(ConfigurationManager.AppSettings["DbType"]);
            dalAllMarket.Open(ConfigurationManager.AppSettings["SQLserverCnnStr"]);
            try
            {
                dalAllMarket.InsertHoseMarketFromRawData(transDate);
            }
            catch
            {
                throw;
            }
            finally
            {
                dalAllMarket.Close();
            }
        }

        public void DeleteBackupAllMarket(DateTime transDate)
        {
            dalAllMarket = StockTrader.DAL.Factory.AllMarketFactory.Create(ConfigurationManager.AppSettings["DbType"]);
            try
            {
                dalAllMarket.Open(ConfigurationManager.AppSettings["SQLserverCnnStr"]);
                dalAllMarket.DeleteBackupAllMarket(transDate);
            }
            catch
            {
                throw;
            }
            finally
            {
                dalAllMarket.Close();
            }

        }

        public List<SecuritiesCompanyUI> getSecuritiesCompany()
        {
            dalAllMarket = StockTrader.DAL.Factory.AllMarketFactory.Create(ConfigurationManager.AppSettings["DbType"]);
            try
            {
                dalAllMarket.Open(ConfigurationManager.AppSettings["SQLserverCnnStr"]);
                List<SecuritiesCompanyUI> list = new List<SecuritiesCompanyUI>();
                List<SecuritiesCompany> result = dalAllMarket.getSecuritiesCompany();
                if (result == null)
                    return null;
                foreach (SecuritiesCompany item in result)
                    list.Add(new SecuritiesCompanyUI(item.TID, item.Name, item.ShortName, item.Address, item.Website, item.MemberSince));
                return list;
            }
            catch
            {
                throw;
            }
            finally
            {
                dalAllMarket.Close();
            }
        }

        public SecuritiesCompanyUI getSecurityCompanyByTID(string TID)
        {
            dalAllMarket = StockTrader.DAL.Factory.AllMarketFactory.Create(ConfigurationManager.AppSettings["DbType"]);
            try
            {
                dalAllMarket.Open(ConfigurationManager.AppSettings["SQLserverCnnStr"]);
                SecuritiesCompany item = dalAllMarket.getSecurityCompanyByTID(TID);
                if (item == null)
                    return null;
                return new SecuritiesCompanyUI(item.TID, item.Name, item.ShortName, item.Address, item.Website, item.MemberSince);
            }
            catch
            {
                throw;
            }
            finally
            {
                dalAllMarket.Close();
            }
        }
        public SecuritiesCompanyUI getSecurityCompanyByShortName(string ShortName)
        {
            dalAllMarket = StockTrader.DAL.Factory.AllMarketFactory.Create(ConfigurationManager.AppSettings["DbType"]);
            try
            {
                dalAllMarket.Open(ConfigurationManager.AppSettings["SQLserverCnnStr"]);
                SecuritiesCompany item = dalAllMarket.getSecurityCompanyByShortName(ShortName);
                if (item == null)
                    return null;
                return new SecuritiesCompanyUI(item.TID, item.Name, item.ShortName, item.Address, item.Website, item.MemberSince);
            }
            catch
            {
                throw;
            }
            finally
            {
                dalAllMarket.Close();
            }
        }

        public HoseMarketSummaryUI getHoseMarketSummary(DateTime fromDate, DateTime toDate, string orderNo, string side, string symbol, long HighPrice, long LowPrice, string PC, string accountNo, string status, string TID, string Tno, string orderType, int fromTime, int toTime)
        {
            dalAllMarket = StockTrader.DAL.Factory.AllMarketFactory.Create(ConfigurationManager.AppSettings["DbType"]);
            dalAllMarket.Open(ConfigurationManager.AppSettings["SQLserverCnnStr"]);
            try
            {
                HoseMarketSummaryData result = dalAllMarket.getHoseMarketSummary(fromDate, toDate, orderNo, side, symbol, HighPrice, LowPrice, PC, accountNo, status, TID, Tno, orderType, fromTime, toTime);
                return new HoseMarketSummaryUI(result.Vol, result.Val, result.Mvol, result.MVal);
            }
            catch
            {
                throw;
            }
            finally
            {
                dalAllMarket.Close();
            }

        }

        public DataTable getHoseMarketOrders(DateTime fromDate, DateTime toDate, string orderNo, string side, string symbol, long HighPrice, long LowPrice, long HighQtty, long LowQtty, long HighMQtty, long LowMQtty, string PC, string accountNo, string status, string TID, string Tno, string orderType, int fromTime, int toTime, double HighVal, double LowVal, double HighmVal, double LowmVal)
        {
            dalAllMarket = StockTrader.DAL.Factory.AllMarketFactory.Create(ConfigurationManager.AppSettings["DbType"]);
            dalAllMarket.Open(ConfigurationManager.AppSettings["SQLserverCnnStr"]);
            try
            {
                List<HoseMarketUI> list = new List<HoseMarketUI>();
                List<HoseMarketData> result = dalAllMarket.getHoseMarketOrders(fromDate, toDate, orderNo, side, symbol, HighPrice, LowPrice, HighQtty, LowQtty, HighMQtty, LowMQtty, PC, accountNo, status, TID, Tno, orderType, fromTime, toTime, HighVal, LowVal, HighmVal, LowmVal);
                if (result == null)
                    return null;
                foreach (HoseMarketData item in result)
                {
                    string companyName = ((getSecurityCompanyByTID(item.Tid) == null) ?
                                    String.Empty : getSecurityCompanyByTID(item.Tid).Name);
                    HoseMarketUI convertedItem = new HoseMarketUI(item.TransDate, item.Side, item.Symbol, item.Price, item.Pc, item.AccountNo, item.Status, item.Time, item.OrderType, item.Vol, item.Mvol, companyName);
                    list.Add(convertedItem);
                }
                return convert2Table<HoseMarketUI>(list);
            }
            catch
            {
                throw;
            }
            finally
            {
                dalAllMarket.Close();
            }
        }

        public void InitSessionData(DateTime transDate)
        {
            dalSecurities = StockTrader.DAL.Factory.SecuritiesFactory.Create(ConfigurationManager.AppSettings["DbType"]);
            try
            {
                dalSecurities.Open(ConfigurationManager.AppSettings["SQLserverCnnStr"]);
                dalSecurities.InitSessionData(transDate);
            }
            catch
            {
                throw;
            }
            finally
            {
                dalSecurities.Close();
            }

        }
        public void UpdateATOByStockNo(int stockNo, DateTime transDate)
        {
            dalSecurities = StockTrader.DAL.Factory.SecuritiesFactory.Create(ConfigurationManager.AppSettings["DbType"]);
            try
            {
                dalSecurities.Open(ConfigurationManager.AppSettings["SQLserverCnnStr"]);
                dalSecurities.UpdateATOByStockNo(stockNo, transDate);
            }
            catch
            {
                throw;
            }
            finally
            {
                dalSecurities.Close();
            }

        }
        public void UpdateATCByStockNo(int stockNo, DateTime transDate)
        {
            dalSecurities = StockTrader.DAL.Factory.SecuritiesFactory.Create(ConfigurationManager.AppSettings["DbType"]);
            try
            {
                dalSecurities.Open(ConfigurationManager.AppSettings["SQLserverCnnStr"]);
                dalSecurities.UpdateATCByStockNo(stockNo, transDate);
            }
            catch
            {
                throw;
            }
            finally
            {
                dalSecurities.Close();
            }
        }
        public void UpdateSession2ByStockNo(int stockNo, DateTime transDate)
        {
            {
                dalSecurities = StockTrader.DAL.Factory.SecuritiesFactory.Create(ConfigurationManager.AppSettings["DbType"]);
                try
                {
                    dalSecurities.Open(ConfigurationManager.AppSettings["SQLserverCnnStr"]);
                    dalSecurities.UpdateSession2ByStockNo(stockNo, transDate);
                }
                catch
                {
                    throw;
                }
                finally
                {
                    dalSecurities.Close();
                }
            }
        }
        public List<int> SelectStockNoByTransDate(DateTime transDate)
        {
            dalAllMarket = StockTrader.DAL.Factory.AllMarketFactory.Create(ConfigurationManager.AppSettings["DbType"]);
            try
            {
                dalAllMarket.Open(ConfigurationManager.AppSettings["SQLserverCnnStr"]);
                return dalAllMarket.SelectStockNoByTransDate(transDate);
            }
            catch
            {
                throw;
            }
            finally
            {
                dalAllMarket.Close();
            }

        }

        public string getSecuritiesNameBySymbol(string symbol)
        {
            dalSecurities = StockTrader.DAL.Factory.SecuritiesFactory.Create(ConfigurationManager.AppSettings["DbType"]);
            try
            {
                dalSecurities.Open(ConfigurationManager.AppSettings["SQLserverCnnStr"]);
                return dalSecurities.getSecuritiesNameBySymbol(symbol);
            }
            catch
            {
                throw;
            }
            finally
            {
                dalSecurities.Close();
            }
        }
        public List<string> getSecuritiesSymbol()
        {
            dalSecurities = StockTrader.DAL.Factory.SecuritiesFactory.Create(ConfigurationManager.AppSettings["DbType"]);
            try
            {
                dalSecurities.Open(ConfigurationManager.AppSettings["SQLserverCnnStr"]);
                return dalSecurities.getSecuritiesSymbol();
            }
            catch
            {
                throw;
            }
            finally
            {
                dalSecurities.Close();
            }
        }

        public int CountBackupSecurities(DateTime transDate)
        {
            dalSecurities = StockTrader.DAL.Factory.SecuritiesFactory.Create(ConfigurationManager.AppSettings["DbType"]);
            try
            {
                dalSecurities.Open(ConfigurationManager.AppSettings["SQLserverCnnStr"]);
                return dalSecurities.CountBackupSecurities(transDate);
            }
            catch
            {
                throw;
            }
            finally
            {
                dalSecurities.Close();
            }
        }

        public static DataTable convert2Table<T>(List<T> list)
        {
            DataTable table = new DataTable();
            if (list.Count > 0)
            {
                PropertyInfo[] properties = list[0].GetType().GetProperties();
                List<string> columns = new List<string>();
                foreach (PropertyInfo pi in properties)
                {
                    Type t = pi.PropertyType;
                    table.Columns.Add(pi.Name, t);
                    columns.Add(pi.Name);
                }
                foreach (T item in list)
                {
                    object[] cells = getValues(columns, item);
                    table.Rows.Add(cells);
                }
            }
            return table;
        }
        private static object[] getValues(List<string> columns, object instance)
        {
            object[] ret = new object[columns.Count];
            for (int n = 0; n < ret.Length; n++)
            {
                PropertyInfo pi = instance.GetType().GetProperty(columns[n]);
                object value = pi.GetValue(instance, null);
                ret[n] = value;
            }
            return ret;
        }
        public void BackupData()
        {
            dalSecurities = StockTrader.DAL.Factory.SecuritiesFactory.Create(ConfigurationManager.AppSettings["DbType"]);
            try
            {
                dalSecurities.Open(ConfigurationManager.AppSettings["SQLserverCnnStr"]);
                //dalSecurities.Backup();
            }
            catch
            {
                throw;
            }
            finally
            {
                dalSecurities.Close();
            }
        }

        public void InsertDailyOrder(int firmno, DateTime transdate, string pcflag, string orderside, string symbol, int volume, int matched, int price, int time)
        {
            dalAllMarket = StockTrader.DAL.Factory.AllMarketFactory.Create(ConfigurationManager.AppSettings["DbType"]);
            dalAllMarket.Open(ConfigurationManager.AppSettings["SQLserverCnnStr"]);
            try
            {
                dalAllMarket.InsertDailyOrder(firmno, transdate, pcflag, orderside, symbol, volume, matched, price, time);

            }
            catch
            {
                throw;
            }
            finally
            {
                dalAllMarket.Close();
            }

        }

        public void InsertDailyOrderWithAccountNo(int firmno, DateTime transdate, string pcflag, string orderside, string symbol, int volume, int matched, int price, int time, string accountno)
        {
            dalAllMarket = StockTrader.DAL.Factory.AllMarketFactory.Create(ConfigurationManager.AppSettings["DbType"]);
            dalAllMarket.Open(ConfigurationManager.AppSettings["SQLserverCnnStr"]);
            try
            {
                dalAllMarket.InsertDailyOrderWithAccountNo(firmno, transdate, pcflag, orderside, symbol, volume, matched, price, time, accountno);

            }
            catch
            {
                throw;
            }
            finally
            {
                dalAllMarket.Close();
            }

        }


        public HoseMarketSummaryUI getDailyOrderSummary(DateTime fromdate, DateTime todate, string firmno, string pcflag, string orderside, string symbol, int lowVolume, int highVolume, int lowMATCHED, int hightMATCHED)
        {
            dalAllMarket = StockTrader.DAL.Factory.AllMarketFactory.Create(ConfigurationManager.AppSettings["DbType"]);
            dalAllMarket.Open(ConfigurationManager.AppSettings["SQLserverCnnStr"]);
            try
            {
                DailyOrderSummaryData result = dalAllMarket.getDailyOrderSummary(fromdate, todate, firmno, pcflag, orderside, symbol, lowVolume, highVolume, lowMATCHED, hightMATCHED);
                return new HoseMarketSummaryUI(result.Vol, 0, result.Mvol, 0);
            }
            catch
            {
                throw;
            }
            finally
            {
                dalAllMarket.Close();
            }

        }

        public DataTable getDailyOrder(DateTime fromdate, DateTime todate, string firmno, string pcflag, string orderside, string symbol, int lowVolume, int highVolume, int lowMVolume, int hightMVolume)
        {
            dalAllMarket = StockTrader.DAL.Factory.AllMarketFactory.Create(ConfigurationManager.AppSettings["DbType"]);
            dalAllMarket.Open(ConfigurationManager.AppSettings["SQLserverCnnStr"]);
            try
            {
                List<HoseMarketUI> list = new List<HoseMarketUI>();
                List<DailyOrderData> result = dalAllMarket.getDailyOrder(fromdate, todate, firmno, pcflag, orderside, symbol, lowVolume, highVolume, lowMVolume, hightMVolume);
                if (result == null)
                    return null;

                foreach (DailyOrderData item in result)
                {
                    string companyName = ((getSecurityCompanyByTID(item.FirmNo.ToString()) == null) ?
                                    String.Empty : getSecurityCompanyByTID(item.FirmNo.ToString()).Name);

                    HoseMarketUI convertedItem = new HoseMarketUI(item.TransDate, item.OrderSide, item.Symbol, item.Price, item.PcFlag, string.Empty, string.Empty, item.Time, string.Empty, item.Volume, item.Matched, companyName);
                    list.Add(convertedItem);
                }
                return convert2Table<HoseMarketUI>(list);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dalAllMarket.Close();
            }
        }

        public HoseMarketSummaryUI getDailyOrderSummaryWithAccountNo(DateTime fromdate, DateTime todate, string firmno, string pcflag, string orderside, string symbol, int lowVolume, int highVolume, int lowMATCHED, int hightMATCHED, string accountno)
        {
            dalAllMarket = StockTrader.DAL.Factory.AllMarketFactory.Create(ConfigurationManager.AppSettings["DbType"]);
            dalAllMarket.Open(ConfigurationManager.AppSettings["SQLserverCnnStr"]);
            try
            {
                DailyOrderSummaryData result = dalAllMarket.getDailyOrderSummaryWithAccountNo(fromdate, todate, firmno, pcflag, orderside, symbol, lowVolume, highVolume, lowMATCHED, hightMATCHED, accountno);
                return new HoseMarketSummaryUI(result.Vol, 0, result.Mvol, 0);
            }
            catch
            {
                throw;
            }
            finally
            {
                dalAllMarket.Close();
            }

        }

        public DataTable getDailyOrderWithAccountNo(DateTime fromdate, DateTime todate, string firmno, string pcflag, string orderside, string symbol, int lowVolume, int highVolume, int lowMVolume, int hightMVolume, string accountno)
        {
            dalAllMarket = StockTrader.DAL.Factory.AllMarketFactory.Create(ConfigurationManager.AppSettings["DbType"]);
            dalAllMarket.Open(ConfigurationManager.AppSettings["SQLserverCnnStr"]);
            try
            {
                List<HoseMarketUI> list = new List<HoseMarketUI>();
                List<DailyOrderData> result = dalAllMarket.getDailyOrderWithAccountNo(fromdate, todate, firmno, pcflag, orderside, symbol, lowVolume, highVolume, lowMVolume, hightMVolume, accountno);
                if (result == null)
                    return null;

                foreach (DailyOrderData item in result)
                {
                    string companyName = ((getSecurityCompanyByTID(item.FirmNo.ToString()) == null) ?
                                    String.Empty : getSecurityCompanyByTID(item.FirmNo.ToString()).Name);

                    HoseMarketUI convertedItem = new HoseMarketUI(item.TransDate, item.OrderSide, item.Symbol, item.Price, item.PcFlag, item.AccountNo, string.Empty, item.Time, string.Empty, item.Volume, item.Matched, companyName);
                    list.Add(convertedItem);
                }
                return convert2Table<HoseMarketUI>(list);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dalAllMarket.Close();
            }
        }

        public void DeleteDailyOrderByDate(DateTime transDate)
        {
            dalAllMarket = StockTrader.DAL.Factory.AllMarketFactory.Create(ConfigurationManager.AppSettings["DbType"]);
            try
            {
                dalAllMarket.Open(ConfigurationManager.AppSettings["SQLserverCnnStr"]);
                dalAllMarket.DeleteDailyOrderByDate(transDate);
            }
            catch
            {
                throw;
            }
            finally
            {
                dalAllMarket.Close();
            }

        }

    }
}
